package com.johnny.xia.util.base;

import java.util.Random;

import org.apache.commons.lang.RandomStringUtils;

public class RandUtil {
	private static Random randomNumberGenerator;

	private static final String STR_SOURCES = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
	static {
		randomNumberGenerator = new Random(System.currentTimeMillis());
	}

	public static int RandInt(int min, int max) {
		return (int) (randomNumberGenerator.nextDouble() * (max - min)) + min;
	}

	public static float RandFloat(float min, float max) {
		return (float) (randomNumberGenerator.nextDouble() * (max - min)) + min;
	}

	// 闭区间
	public static int RandInt(int[] r) {
		return RandInt(r[0], r[1] + 1);
	}

	public static int Rand(int[] rate, int total) {
		int r = RandInt(0, total);
		int t = 0;
		for (int i = 0; i < rate.length; i++) {
			t += rate[i];
			if (r < t) {
				return i;
			}
		}
		throw new RuntimeException("error random");
	}

	public static int[] Rand2(int[] rate, int total) {
		int r = RandInt(0, total) + 1;
		int t = 0;
		for (int i = 0; i < rate.length; i++) {
			t += rate[i];
			if (r < t) {
				return new int[] { i, r };
			}
		}
		throw new RuntimeException("error random");
	}

	public static String randStr(int len) {
		return RandomStringUtils.random(len, STR_SOURCES);
	}

	public static void main(String[] args) {
		for (int i = 1; i <= 48; i++) {
			System.out.println(randStr(i));
		}
	}
}
